package com.mifengkong.statistic.umengsignin;

import android.app.Activity;

import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frtools.util.FRToast;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.media.UMusic;

import java.util.Map;

/**
 * Created by jack on 2018/4/2
 * umeng 工具类
 */

public class WeChatSignInManager {

    private Activity activity;
    private String title;
    private String desc;
    private String url;
    private String thumpImg;
    private String imageUrl;
    private String videoUrl;
    private String musicUrl;

    public WeChatSignInManager(Activity activity) {
        this.activity = activity;
    }

    public static WeChatSignInManager with(Activity activity) {
        return new WeChatSignInManager(activity);
    }

    public void initWeChatLogin(final OnWeChatAuthListener onWeChatAuthListener) {
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.WEIXIN, new UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {

            }

            @Override
            public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
                if (null != onWeChatAuthListener) {
                    onWeChatAuthListener.onSuccess(share_media, map);
                }
            }

            @Override
            public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                if (null != onWeChatAuthListener) {
                    onWeChatAuthListener.onError(share_media, throwable);
                }
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media, int i) {

            }
        });
    }

    interface OnWeChatAuthListener {

        /**
         * @param platform 平台名称
         * @param dataMap  用户资料返回
         */
        void onSuccess(SHARE_MEDIA platform, Map<String, String> dataMap);

        void onError(SHARE_MEDIA platform, Throwable throwable);
    }

    public WeChatSignInManager shareTitle(String title) {
        this.title = title;
        return this;
    }

    public WeChatSignInManager shareDesc(String desc) {
        this.desc = desc;
        return this;
    }

    public WeChatSignInManager shareUrl(String url) {
        this.url = url;
        return this;
    }

    public WeChatSignInManager shareThumpImg(String thumpImg) {
        this.thumpImg = thumpImg;
        return this;
    }

    public WeChatSignInManager shareImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
        return this;
    }

    public WeChatSignInManager shareVideoUrl(String videoUrl) {
        this.videoUrl = videoUrl;
        return this;
    }

    public WeChatSignInManager shareMusicUrl(String musicUrl) {
        this.musicUrl = musicUrl;
        return this;
    }

    public void shareView(final OnShareViewListener onShareViewListener) {
        UMImage thumb = new UMImage(activity, thumpImg);
        ShareAction shareAction;
        if (!FRString.isEmpty(musicUrl)) {
            UMusic shareMusic = new UMusic(musicUrl);
            shareMusic.setTitle(title);//音乐的标题
            shareMusic.setThumb(thumb);//音乐的缩略图
            shareMusic.setDescription(desc);//音乐的描述
            shareAction = new ShareAction(activity)
                    .withMedia(shareMusic);
        } else if (!FRString.isEmpty(videoUrl)) {
            UMVideo shareVideo = new UMVideo(videoUrl);
            shareVideo.setTitle(title);//视频的标题
            shareVideo.setThumb(thumb);//视频的缩略图
            shareVideo.setDescription(desc);//视频的描述
            shareAction = new ShareAction(activity)
                    .withMedia(shareVideo);
        } else if (!FRString.isEmpty(imageUrl)) {
            UMImage shareImage = new UMImage(activity, imageUrl);//网络图片
            shareImage.setThumb(thumb);
            shareAction = new ShareAction(activity)
                    .withMedia(shareImage);
        } else if (!FRString.isEmpty(url)) {
            UMWeb shareWeb = new UMWeb(url);
            shareWeb.setTitle(title);//标题
            shareWeb.setThumb(thumb);  //缩略图
            shareWeb.setDescription(desc);//描述
            shareAction = new ShareAction(activity)
                    .withMedia(shareWeb);
        } else {
            throw new IllegalArgumentException("请至少分享一种");
        }
        shareAction.setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
                SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QZONE,
                SHARE_MEDIA.WEIXIN_CIRCLE)
                .setCallback(new UMShareListener() {
                    @Override
                    public void onStart(SHARE_MEDIA share_media) {
                        if (null != onShareViewListener) {
                            onShareViewListener.onShareStart();
                        }
                    }

                    @Override
                    public void onResult(SHARE_MEDIA share_media) {
                        if (null != onShareViewListener) {
                            onShareViewListener.onShareFinish();
                        }
                    }

                    @Override
                    public void onError(SHARE_MEDIA share_media, Throwable t) {
                        if (t != null) {
                            if (t.getMessage().contains("没有安装应用")) {
                                switch (share_media.toString()) {
                                    case "SINA":
                                        FRToast.showToastSafe("请先安装微博");
                                        break;
                                    case "QQ":
                                    case "QZONE":
                                        FRToast.showToastSafe("请先安装QQ");
                                        break;
                                    case "WEIXIN":
                                    case "WEIXIN_CIRCLE":
                                        FRToast.showToastSafe("请先安装微信");
                                        break;
                                }
                                if (null != onShareViewListener) {
                                    onShareViewListener.onShareFinish();
                                }
                            }
                        }
                    }

                    @Override
                    public void onCancel(SHARE_MEDIA share_media) {
                        if (null != onShareViewListener) {
                            onShareViewListener.onShareFinish();
                        }
                    }
                }).open();
    }

    interface OnShareViewListener {
        void onShareStart();

        void onShareFinish();
    }
}
